package com.wxproject.demo.Controller.Admin;

import java.util.List;

import com.wxproject.demo.Common.MyException.BusinessException;
import com.wxproject.demo.Common.exception.ResultEnum;
import com.wxproject.demo.Common.lang.ResultBody;
import com.wxproject.demo.Entity.AO.ArticleTypeAO;
import com.wxproject.demo.Entity.AO.ArticleTypeAlterAO;
import com.wxproject.demo.Entity.VO.ArticleTypeVO;
import com.wxproject.demo.Service.ArticleTypeService;
import com.wxproject.demo.Utils.DateUtils;
import com.wxproject.demo.Utils.SnowFlake;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zqf
 * @date 2021/4/9 20:35
 * @description 添加文章分类，并创建该分类的聊天室
 * 修改文章分类
 */
@RestController
@Log4j2
@RequestMapping("/admin")
public class AdminArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;

    //管理员获得所有文章分类
    @GetMapping("/getAllArticleType")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    public ResultBody getAllArticleType() {
        List<ArticleTypeVO> typeList = articleTypeService.getAllArticleType();
        return ResultBody.ok().data("typeList", typeList);
    }

    //添加文章分类
    @PostMapping("/addArticleType")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    public ResultBody addArticleType(@RequestParam("articleTypeName") String articleTypeName) {
        ArticleTypeAO articleTypeAO = new ArticleTypeAO();
        articleTypeAO.setTypeName(articleTypeName);
        articleTypeAO.setChatRoomId(String.valueOf(SnowFlake.nextId()));
        articleTypeAO.setArticleId(String.valueOf(SnowFlake.nextId()));
        try {
            Integer res = articleTypeService.addArticleType(articleTypeAO);
            Integer ans = articleTypeService.addChatRoom(articleTypeAO.getChatRoomId(), articleTypeAO.getTypeName(), articleTypeAO.getArticleId(), DateUtils.getDate());
            if (res != 0 && ans != 0) {
                return ResultBody.ok().message("添加成功");
            } else {
                return ResultBody.error().message("该类型不存在");
            }
        } catch (Exception e) {
            log.info("该类型已存在", e);
            throw new BusinessException(ResultEnum.EXIST_ARTICLE_TYPE);
        }
    }

    //修改类型
    @PostMapping("/alterType")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    public ResultBody alertType(@RequestBody ArticleTypeAlterAO articleTypeAlterAO) {
        try {
            Integer res = articleTypeService.alertType(articleTypeAlterAO);
            if (res != 0) {
                return ResultBody.ok().message("修改成功");
            } else {
                return ResultBody.error().message("该类型不存在");
            }
        } catch (Exception e) {
            log.info("该类型已存在", e);
            throw new BusinessException(ResultEnum.EXIST_ARTICLE_TYPE);
        }
    }

    @PostMapping("/deleteType/{id}")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    public ResultBody deleteType(@PathVariable("id") String id) {
        Integer res = articleTypeService.deleteType(id);
        if (res != 0) {
            return ResultBody.ok().message("删除成功");
        } else {
            return ResultBody.error().message("分类已呗删除");
        }
    }

}
